package de.clue.corpus.twitter.langident;

import java.util.Map;

import org.knallgrau.utils.textcat.TextCategorizer;

/**
 * Class used identify the language 'german' using textcat.
 * 
 * @author asuslova
 *
 */
public class LangIdentTextCat implements ILanguageIdentification {
	
	
	private int distancesDeviation = -99;
    private TextCategorizer guesser;

	public int getDistancesDeviation() {
		return distancesDeviation;
	}

	public String getLanguage(String text){
		
		String language = "";
		
		distancesDeviation = -99; // resetting
		
		try {
		    
		       
			
	           String category = "";
	           guesser = new TextCategorizer();
	           category = guesser.categorize(text);
	           Map<String, Integer> categoryDistances = guesser.getCategoryDistances(text);
	           Integer integer = categoryDistances.get("german");
	           distancesDeviation = distancesDeviation(categoryDistances, integer);
	           
	           language = category;
	           
	           
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return language;
		
	}
	
	/**
	 * Get the maximum deviation from the minimum distance to the maxium distance.
	 * 
	 */
	private int distancesDeviation(Map<String, Integer> categoryDistances, int minimalDistance){
		
		int maxDeviation = 0;
		
		for(Integer currentValue:categoryDistances.values()){
			int curDif = currentValue.intValue() - minimalDistance;
			if(curDif > maxDeviation){
				maxDeviation = curDif;
			}
			
		}
		
		
		return maxDeviation;
	}
	

}
